#ifdef a
#include"SListNode.h"
void TestNode() {
	SLNode* pplist = NULL;
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	//插入测试s
	//尾插
	SListPushBack(&pplist, 0);
	SListPushBack(&pplist, 1);
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	//头插
	SListPushHead(&pplist, -1);
	SListPushHead(&pplist, -2);
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	//-2 -1 0 1
	//任意位置插入(同时测试查找函数)
	SListPushPos(SListSearch(pplist, -1), 5);
	SListPushPos(SListSearch(pplist, 0), 5);
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
//----------- -2 -1 5 0 5 1
	//删除测试
	//尾删
	SListPopBack(&pplist);
	//SListPopBack(&pplist);
	//SListPopBack(&pplist);
	//SListPopBack(&pplist);
	//SListPopBack(&pplist);
	//SListPopBack(&pplist);
	//-2 -1 5 0	5
	//头删
	SListPopHead(&pplist);
	SListPopHead(&pplist);
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	//任意位置后删除  5 0 5
	SListPopPos(SListSearch(pplist, 5));
	SListPopPos(SListSearch(pplist, 5));
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	//销毁测试
	SListDestory(&pplist);
	PrintSList(pplist);
	printf("size = %d\n\n", SListSize(pplist));
	return;
}

//--------------------------------------------------------------
 
//节点创建
SLNode* BuySListNode(TYPENODE data) {
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (!newnode) {
		assert(0);
		return NULL;
	}
	newnode->next = NULL;
	newnode->val = data;
	return newnode;
}

//尾插
void SListPushBack(SLNode** pplist, TYPENODE val) {
	assert(pplist);
	SLNode* newnode = BuySListNode(val);
	//空
	if (!*pplist) 
		*pplist = newnode;
	//非空
	else {
		SLNode* cur = *pplist;
		while (cur->next)
			cur = cur->next;
		cur->next = newnode;
	}
	return;
}


//尾删
void SListPopBack(SLNode** pplist) {
	assert(pplist);
	//空
	if (!*pplist)
		return;
	//只有一个节点
	else if(!((*pplist)->next)){
		free(*pplist);
		*pplist = NULL;
	}
	else {
	SLNode* cur = *pplist;
	SLNode* prev = NULL;
	while (cur->next) {
		prev = cur;
		cur = cur->next;
	}
	free(cur);
	prev->next = NULL;
	}
	return;
}

//头插
void SListPushHead(SLNode** pplist, TYPENODE data) {
	assert(pplist);
	SLNode* newnode = BuySListNode(data);
	if (!newnode) {
		assert(0);
		return;
	}
	//空
	if (!*pplist) 
		*pplist = newnode;
	else {
		newnode->next = *pplist;
		*pplist =  newnode;
	}
	return;
}

//头删
void SListPopHead(SLNode** pplist) {
	assert(pplist);
	//空
	if (!*pplist)
		return;
	else {
		SLNode* delnode = *pplist;
		*pplist = delnode->next;
		free(delnode);
		delnode = NULL;
	}
	return;
}

//有效节点个数
int SListSize(SLNode* pplist) {
	SLNode* cur = pplist;
	int count = 0;
	while (cur) {
		cur = cur->next;
		count++;
	}
	return count;
}

//打印
void PrintSList(SLNode* plist) {
	SLNode* cur = plist;
	while (cur) {
		printf("%d->", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
	return;
}

//pos节点后插入
void SListPushPos(SLNode* pos, TYPENODE data) {
	if (!pos)
		return;
	//不能在NULL节点后插入
	SLNode* newnode = BuySListNode(data);
	newnode->next = pos->next;
	pos->next = newnode;
	return;
}

//pos节点后删除
void SListPopPos(SLNode* pos) {
	if (!pos || !(pos->next))
		return;
	SLNode* delnode = pos->next;
	pos->next = delnode->next;
	free(delnode);
	delnode = NULL;
	return;
}

//查找值域为data的第一个节点并返回其下标。
SLNode* SListSearch(SLNode* plist, TYPENODE data) {
	if (!plist)
		return NULL;
	SLNode* cur = plist;
	while (cur) {
		if (cur->val == data)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

//连表销毁
void SListDestory(SLNode** pplist) {
	assert(pplist);
	SLNode* delnode = *pplist;
	while (*pplist) {
		*pplist = delnode->next;
		free(delnode);
		delnode = *pplist;
	}
	return;
}
#endif 